package spring.in.action.chapter_01.life;

import java.io.PrintStream;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

/**
 * 验证Spring Bean 生命周期的bean
 * 
 * @author Liucm
 *
 */
@Component(value = "lifeBean")
public class LifeBean implements BeanNameAware, BeanFactoryAware, ApplicationContextAware,
        InitializingBean, DisposableBean
{

    int index = 0;

    PrintStream out = null;

    public LifeBean(PrintStream pOut)
    {
        this.out = pOut;
        out.println((++index) + "、实例化");
    }

    @Autowired
    public void setOut(PrintStream pOut)
    {
        this.out = pOut;
        out.println((++index) + "、属性注入");
    }

    public int getIndex()
    {
        return this.index;
    }

    public void setIndex(int pIndex)
    {
        this.index = pIndex;
    }

    public PrintStream getOut()
    {
        return this.out;
    }

    @Override
    public void destroy() throws Exception
    {
        out.println((++index) + "、DisposableBean.destroy");
    }

    @Override
    public void afterPropertiesSet() throws Exception
    {
        out.println((++index) + "、InitializingBean.afterPropertiesSet");

    }

    @Override
    public void setApplicationContext(ApplicationContext pApplicationContext) throws BeansException
    {
        out.println((++index) + "、ApplicationContextAware.setApplicationContext");
    }

    @Override
    public void setBeanFactory(BeanFactory pBeanFactory) throws BeansException
    {
        out.println((++index) + "、BeanFactoryAware.setBeanFactory");
    }

    @Override
    public void setBeanName(String pName)
    {
        out.println((++index) + "、BeanNameAware.setBeanName");
    }

    @PreDestroy
    public void PreDestroy()
    {
        out.println((++index) + "、@interface.PreDestroy");
    }

    @PostConstruct
    public void PostConstruct()
    {
        out.println((++index) + "、@interface.PostConstruct");
    }

    public void DefatuInitMethod()
    {
        out.println((++index) + "、xml.default-init-method，没有xml.init-method时执行，不实现不会报错");
    }

    public void DefatuDestroyMethod()
    {
        out.println((++index) + "、xml.default-destory-method，没有xml.default-method时执行，不实现不会报错");
    }

    public void InitMethod()
    {
        out.println((++index) + "、xml.init-method，会覆盖掉xml.default-init-method的执行，不实现会报错");
    }

    public void DestroyMethod()
    {
        out.println((++index) + "、xml.destory-method，会覆盖掉xml.default-destory-method，不实现会报错");
    }

    public static void main(String[] args)
    {
        ClassPathXmlApplicationContext lApplicationContext;
        System.out.println("-----------------Spring 生命周期xml版---------------------");
        lApplicationContext = new ClassPathXmlApplicationContext("chapter_1_LifeBean1.xml");
        lApplicationContext.getBean("lifeBean");
        lApplicationContext.close();

        System.out.println();

        System.out.println("-----------------Spring 生命周期注解版---------------------");
        lApplicationContext = new ClassPathXmlApplicationContext("chapter_1_LifeBean2.xml");
        lApplicationContext.getBean("lifeBean");
        lApplicationContext.close();
    }
}
